// 初始化工具提示
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
    return new bootstrap.Tooltip(tooltipTriggerEl)
});

// 初始化轮播图
document.addEventListener('DOMContentLoaded', function() {
    // 获取轮播图元素
    const carousel = document.getElementById('mainCarousel');
    if (carousel) {
        // 创建轮播图实例
        const bsCarousel = new bootstrap.Carousel(carousel, {
            interval: 5000,  // 5秒切换一次
            wrap: true,      // 循环播放
            keyboard: true,  // 支持键盘控制
            pause: 'hover'   // 鼠标悬停时暂停
        });
    }
});

// 懒加载和无限滚动
$(document).ready(function() {
    let page = 1;
    let loading = false;
    const loadMoreBtn = $('#loadMoreBtn');

    // 加载更多文章
    function loadMorePosts() {
        if (loading) return;
        
        loading = true;
        loadMoreBtn.addClass('loading');
        
        $.ajax({
            url: '/api/posts/more',
            type: 'GET',
            data: { page: page },
            success: function(res) {
                if (res.code === 200) {
                    const posts = res.data;
                    if (posts.length > 0) {
                        posts.forEach(post => {
                            const postHtml = `
                                <div class="masonry-item">
                                    <div class="card post-card">
                                        <img src="${post.firstImage}" class="card-img-top" alt="${post.title}" loading="lazy">
                                        <div class="card-body">
                                            <h5 class="card-title">${post.title}</h5>
                                            <p class="card-text">${post.summary}</p>
                                            <div class="post-meta">
                                                <div class="d-flex align-items-center">
                                                    <img src="${post.authorAvatar}" class="avatar-img rounded-circle me-2" loading="lazy">
                                                    <span class="text-muted">${post.authorName}</span>
                                                </div>
                                                <div class="post-stats">
                                                    <span class="me-3">
                                                        <i class="fas fa-heart text-danger"></i>
                                                        <span>${post.likes}</span>
                                                    </span>
                                                    <span>
                                                        <i class="fas fa-eye text-primary"></i>
                                                        <span>${post.views}</span>
                                                    </span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            `;
                            $('#postGrid').append(postHtml);
                        });
                        page++;
                    } else {
                        loadMoreBtn.text('没有更多内容了').prop('disabled', true);
                    }
                }
            },
            complete: function() {
                loading = false;
                loadMoreBtn.removeClass('loading');
            }
        });
    }

    // 点击加载更多
    loadMoreBtn.on('click', loadMorePosts);

    // 滚动到底部自动加载
    $(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() > $(document).height() - 100) {
            loadMorePosts();
        }
    });
}); 